package com.aba.auth.control;

import cn.hutool.core.util.StrUtil;
import com.aba.auth.common.R;
import com.aba.auth.entity.User;
import com.aba.auth.service.impl.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @title: UserController
 * @Author YuanJC
 * @Date: 2024/3/13
 * @Version 1.0
 */
@RestController
@Slf4j
@Api(value = "用户")
@RequestMapping("auth")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public R<Page<User>> page(int page, int pageSize, String username) {
        Page<User> userPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StrUtil.isNotBlank(username),User::getUsername,username);
        return R.ok(userService.page(userPage,lambdaQueryWrapper));
    }

    @PostMapping
    public R<Boolean> save(@RequestBody User user) {
        return R.ok(userService.save(user));
    }

    @DeleteMapping
    public R<Boolean> delete(long id) {
        return R.ok(userService.removeById(id));
    }

    @PutMapping
    public R<Boolean> update(@RequestBody User user) {
        return R.ok(userService.updateById(user));
    }
}